<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Repository\UserTokenRepository;
use Dingo\Api\Exception\StoreResourceFailedException;

/**
 * Class TokenController
 * @package App\Http\Controllers\Api\V1
 */
class TokenController extends Controller
{
    /**
     * @var UserTokenRepository
     */
    public $token;

    /**
     * 获取token
     * @return string
     */
    public function getToken()
    {
        $code = app('request')->only('code');
        $rules = ['code' => 'required'];
        $validator = app('validator')->make($code, $rules);

        if ($validator->fails()){
            throw new StoreResourceFailedException('code值不能为空', null, null, [], 10003);
        }

        $UserTokenRepository = new UserTokenRepository($code);
        $token = $UserTokenRepository->get();
        return json_encode(['token' => $token]);
    }
}
